/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/14 01:43
 */
package org.mechanic.fund.domain.system.manager;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.builder.ToStringBuilder;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户部门实体
 *
 * @author mechanic
 * @version 0.0.1
 */
@Entity
@Table(name = "sys_department")
public class Department {

    private static final long serialVersionUID = 6304691447736642769L;

    /**
     * 部门id，整型。指定时必须大于1，不指定时则自动生成
     * 对应微信字段：id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY )
    @Column(name = "id", columnDefinition = "BIGINT")
    private String id;
    /**
     * 父级部门id。根部门id为1
     * 对应微信字段：parentid
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "wx_parent_id")
    @JsonIgnoreProperties(value = {"sort", "wxParent", "children"})
    private Department wxParent;

    /**
     * 子部门
     */
    @OneToMany(cascade= CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "wxParent")
    @JsonIgnoreProperties(value = "wxParent")
    private Set<Department> children = new HashSet<Department>();

    /**
     * 部门id，整型。指定时必须大于1，不指定时则自动生成
     * 对应微信字段：id
     */
    //@Column(name = "wx_deptment_id")
    //private Integer wxDeptmentId;
    /**
     * 部门名称
     * 对应微信字段：name
     */
    @Column(name = "name")
    private String name;
    /**
     * 在父部门中的次序值。order值小的排序靠前。
     * 对应微信字段：order
     */
    @Column(name = "sort")
    private Integer sort;

    public Department() {
    }

    public Department(
      String id,
      Department wxParent,
      Set<Department> children,
      String name,
      Integer sort) {
        this.id = id;
        this.wxParent = wxParent;
        this.children = children;
        this.name = name;
        this.sort = sort;
    }

    //@Override
    public String getId() {
        return id;
    }

    //@Override
    public void setId(String id) {
        this.id = id;
    }

    public Department getWxParent() {
        return wxParent;
    }

    public void setWxParent(Department wxParent) {
        this.wxParent = wxParent;
    }

    public Set<Department> getChildren() {
        return children;
    }

    public void setChildren(Set<Department> children) {
        this.children = children;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
